package com.ebook.repository;

import com.ebook.entity.Book;
import com.ebook.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
    List<Book> findByUserOrderByCreatedAtDesc(User user);
    
    List<Book> findByUserAndCategoryOrderByCreatedAtDesc(User user, String category);
    
    @Query("SELECT DISTINCT b.category FROM Book b WHERE b.user = :user AND b.category IS NOT NULL")
    List<String> findCategoriesByUser(@Param("user") User user);
    
    @Query("SELECT b FROM Book b WHERE b.user = :user AND " +
           "(LOWER(b.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " +
           "LOWER(b.author) LIKE LOWER(CONCAT('%', :keyword, '%')))")
    List<Book> searchBooks(@Param("user") User user, @Param("keyword") String keyword);
}